package com.jiyun.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.entity.MaterialType;
import com.jiyun.service.IMaterialTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-02-22
 */
@RestController
@RequestMapping("/material-type")
public class MaterialTypeController {

    @Autowired
    IMaterialTypeService materialTypeService;

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addMaterialType(@RequestBody MaterialType materialType){
        materialTypeService.save(materialType);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateMaterialType(@RequestBody MaterialType materialType){
        materialTypeService.updateById(materialType);
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        materialTypeService.removeById(id);
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        materialTypeService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public MaterialType findById(@RequestParam Integer id){
        return materialTypeService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<MaterialType> findAll(){
        return materialTypeService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<MaterialType> findPage(@RequestParam Integer page,@RequestParam Integer pageSize){
        Page<MaterialType> materialTypePage = new Page<>(page, pageSize);
        return materialTypeService.page(materialTypePage);
    }


    /**
     * [
     *      label:"名称",
     *      value:"1",
     *      children:[
     *
     *      ]
     * ]
     * @return
     */
    @RequestMapping("materialTypeTree")
    public List<Map> materialTypeTree(){
        List<MaterialType> all = materialTypeService.list();


        return findTypeByPid(all,0);
    }

    public List<Map> findTypeByPid(List<MaterialType> all,Integer pid){

        ArrayList<Map> result = new ArrayList<>();


        for (MaterialType materialType : all) {
            if (materialType.getPid().equals(pid)) {

                HashMap hashMap = new HashMap();
                hashMap.put("label",materialType.getName());
                hashMap.put("value",materialType.getId());

                List<Map> son = findTypeByPid(all, materialType.getId());

                if(CollectionUtils.isNotEmpty(son)){
                    hashMap.put("children",son);
                }
                result.add(hashMap);
            }
        }



        return result;
    }
}
